﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApp
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();

            BindOrderByDay();

            Thread t = new Thread(new ThreadStart(delegate() {

                while (true)
                {
                    if (this.checkBox1.Checked)
                    {
                        BindOrderDetailList();
                    }

                    Thread.Sleep(3000);
                }
            
            }));

            t.Start();

        }

        private void FrmMain_Load(object sender, EventArgs e)
        {

        }

        public int NowOrderId
        {
            get;
            set;
        }

        public void BindOrderByDay()
        {
            localhost.WebService client = new localhost.WebService();

            client.CookieContainer = CookieContainer.Data;

            var list = client.GetOrderByYearOfDay(this.dateTimePicker1.Value.DayOfYear);

            this.dataGridView1.DataSource = list;
        }

        public void BindOrderDetailList()
        {
            if (NowOrderId > 0)
            {
                Thread t = new Thread(new ThreadStart(delegate()
                {

                    localhost.WebService client = new localhost.WebService();

                    client.CookieContainer = CookieContainer.Data;

                    var list = client.GetOrderDetailByOrderId(NowOrderId);


                    this.dataGridView2.Invoke(new MethodInvoker(delegate() {

                        this.dataGridView2.DataSource = list;
                        
                    }));
                }));

                t.Start();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FrmTakeOrder t = new FrmTakeOrder();

            t.ShowDialog(this);
        }
    }
}
